#ifndef _ITOA_H_
#define _ITOA_H_

template <typename Type>
std::string Itoa(Type n, int dec = 10)
{
	char buffer[256] = { 0 };
	char* p = buffer;

	if ( n < 0 )
	{
		buffer[0] = '-';
		++ p;
		n = -n;
	}
	do
	{
		*p++ = "0123456789"[n % dec];
		n /= dec;
		if (static_cast<size_t>(p - buffer) > sizeof(buffer)) return "";
	} while (n);
	buffer[p - buffer] = 0;

	int i, j;
	i = buffer[0] == '-' ? 1 : 0;
	j = (int)strlen(buffer) - 1;
	for ( ; i < j; ++ i, -- j )
		std::swap( buffer[i], buffer[j] );

	return buffer;
}

#endif // _ITOA_H_
